package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.WechatMaterial;
import com.jy.adscm.wechat.bean.WechatMaterialDetail;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.mapper.mysql.WechatMaterialDetailMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.service.WechatMaterialDetailService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-30
 */
@Service
public class WechatMaterialDetailServiceImpl extends ServiceImpl<WechatMaterialDetailMapper, WechatMaterialDetail> implements WechatMaterialDetailService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatMaterialService wechatMaterialService;

    @Override
    public Boolean insertWechatMaterialDetail(WechatMaterialDetail wechatMaterialDetail, String openid)throws  AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            throw new AdscmSystemException("不存在该用户");
        }
        wechatMaterialDetail.setOutStocker(wechatUser.getId().toString());
        wechatMaterialDetail.setOutStockTime(new Date());
        boolean insert = super.insert(wechatMaterialDetail);
        //扣减任务中的数量
        WechatMaterial wechatMaterial=    wechatMaterialService.updateWechatMaterialVaule(wechatMaterialDetail.getPid(),wechatMaterialDetail.getNums());
        return insert;
    }

    @Override
    public List<WechatMaterialDetail> selectByPid(Long pid) {
        Wrapper<WechatMaterialDetail> wechatMaterialDetailWrapper=new EntityWrapper<>();
        wechatMaterialDetailWrapper.eq("pid",pid);
        List<WechatMaterialDetail> wechatMaterialDetails = super.selectList(wechatMaterialDetailWrapper);
        for (WechatMaterialDetail wechatMaterialDetail : wechatMaterialDetails) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterialDetail.getOutStocker(),null)){
                WechatUser wechatUser = wechatUserMapper.selectById(wechatMaterialDetail.getOutStocker());
                wechatMaterialDetail.setOutStocker(wechatUser.getName());
            }
        }
        return wechatMaterialDetails;
    }

    @Override
    public WechatMaterialDetail selectWechatMaterialDetailById(Long id) {
        WechatMaterialDetail wechatMaterialDetail = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatMaterialDetail.getOutStocker(),null)){
            WechatUser wechatUser = wechatUserMapper.selectById(wechatMaterialDetail.getOutStocker());
            wechatMaterialDetail.setOutStocker(wechatUser.getName());
        }
        return wechatMaterialDetail;
    }
}
